package com.baizan.daka.ui.activity

import android.view.KeyEvent
import android.view.View
import com.baizan.base.BaseActivity
import com.baizan.daka.AppUpdateManager
import com.baizan.daka.event.HomeEvent
import com.baizan.daka.mvp.model.bean.AppVerSionBean
import com.baizan.daka.showToast
import com.baizan.daka.ui.contract.HomeContract
import com.baizan.daka.ui.fragment.HomeFragment
import com.baizan.daka.ui.fragment.MeFragment
import com.baizan.daka.ui.fragment.TaskFragment
import com.baizan.daka.ui.fragment.VipFragment
import com.baizan.daka.ui.presenter.HomePresenter
import com.baizan.daka.util.FragmentStack
import com.dianzan.daka.R
import kotlinx.android.synthetic.main.act_home.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class HomeActivity : BaseActivity<HomePresenter>(), HomeContract.View,
    View.OnClickListener, AppUpdateManager.OnUpdateAppListener {
    private val stack: FragmentStack by lazy {
        FragmentStack(R.id.content_fl, supportFragmentManager)
    }

    private val mUpdateManager: AppUpdateManager by lazy {
        AppUpdateManager(this)
    }
    private var mHomeFragment: HomeFragment? = null
    private var mTaskFragment: TaskFragment? = null
    private var mVipFragment: VipFragment? = null
    private var mMeFragment: MeFragment? = null

    override fun initPresenter(): HomePresenter {
        return HomePresenter(this)
    }

    override fun layoutId(): Int {
        return R.layout.act_home
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun changeOnClick(event: HomeEvent) {
        when (event.type) {
            2 -> taocan_tv.performClick()
            0 -> home_tv.performClick()
//            2 -> onClick(taocan_tv)
//            0 -> onClick(home_tv)
        }


    }

    override fun onClick(v: View) {
        home_tv.isSelected = false
        renwu_tv.isSelected = false
        taocan_tv.isSelected = false
        wode_tv.isSelected = false
        v?.isSelected = true
        switchFragment(v.id)
    }

    private fun switchFragment(viewId: Int) {
        when (viewId) {
            R.id.home_tv -> mHomeFragment?.let {
                stack.show(it)
            } ?: HomeFragment().let {
                mHomeFragment = it
                stack.show(it)
            }
            R.id.renwu_tv -> mTaskFragment?.let {
                stack.show(it)
            } ?: TaskFragment().let {
                mTaskFragment = it
                stack.show(it)
            }
            R.id.taocan_tv -> mVipFragment?.let {
                stack.show(it)
            } ?: VipFragment().let {
                mVipFragment = it
                stack.show(it)
            }
            R.id.wode_tv -> mMeFragment?.let {
                stack.show(it)
            } ?: MeFragment().let {
                mMeFragment = it
                stack.show(it)
            }
        }
    }

    override fun initData() {
        home_tv.setOnClickListener(this)
        renwu_tv.setOnClickListener(this)
        taocan_tv.setOnClickListener(this)
        wode_tv.setOnClickListener(this)
        home_tv.performClick()


    }

    override fun initView() {
    }

    override fun start() {
        EventBus.getDefault().register(this)
        presenter?.let {
            mUpdateManager.setOnUpdateAppListener(this)
            mUpdateManager.checkApkVersion(it.getCompositeDis())
        }

    }

    override fun onDestroy() {
        EventBus.getDefault().unregister(this)
        stack.eixt()
        super.onDestroy()

    }

    private var mExitTime: Long = 0

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis().minus(mExitTime) <= 2000) {
                finish()
            } else {
                mExitTime = System.currentTimeMillis()
                showToast("再按一次退出程序")
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onUpdate(appCheck: AppVerSionBean) {
        mUpdateManager.showUpdataInfo(appCheck)
    }

    override fun onNoupdate() {
    }


}